home *** CD-ROM | disk | FTP | other *** search
/ Programming Microsoft Visual Basic .NET / Programming Microsoft Visual Basic .NET (Microsoft Press)(X08-78517)(2002).bin / setup / vbnet / 23 web forms and controls / databinding / singlevaluedatabindingform.aspx.vb < prev    next >
Encoding:
Text File  |  2002-02-22  |  2.4 KB  |  61 lines

  1. Imports System.Data.SqlClient
  2.  
  3. Public Class SingleValueDataBindingForm
  4.     Inherits System.Web.UI.Page
  5.     Protected WithEvents txtType As System.Web.UI.WebControls.TextBox
  6.     Protected WithEvents txtPrice As System.Web.UI.WebControls.TextBox
  7.     Protected WithEvents txtTitle As System.Web.UI.WebControls.TextBox
  8.     Protected WithEvents ddlTitles As System.Web.UI.WebControls.DropDownList
  9.  
  10. #Region " Web Form Designer Generated Code "
  11.  
  12.     'This call is required by the Web Form Designer.
  13.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
  14.  
  15.     End Sub
  16.  
  17.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
  18.         'CODEGEN: This method call is required by the Web Form Designer
  19.         'Do not modify it using the code editor.
  20.         InitializeComponent()
  21.     End Sub
  22.  
  23. #End Region
  24.  
  25.     ' Define a Public DataRow variable that is visible from <%# %> expressions.
  26.     Public Titles As DataRow
  27.  
  28.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  29.         ' Fill the 
  30.         If Not Page.IsPostBack Then
  31.             ' Fill the data table and then close the connection.
  32.             Dim cn As New SqlConnection(SqlPubsConnString)
  33.             Dim da As New SqlDataAdapter("SELECT * FROM Titles", cn)
  34.             Dim dt As New DataTable()
  35.             da.Fill(dt)
  36.             ' Store the DataTable in a Session variable.
  37.             Session("TitlesDataTable") = dt
  38.             ' Manually fill the Items collection of the ddlTitles control.
  39.             Dim dr As DataRow
  40.             For Each dr In dt.Rows
  41.                 ddlTitles.Items.Add(dr("title").ToString)
  42.             Next
  43.             ddlTitles.SelectedIndex = 0
  44.             ' Prepare the Titles variable for binding
  45.             Titles = dt.Rows(0)
  46.  
  47.             ' Bind all controls.
  48.             Me.DataBind()
  49.         End If
  50.  
  51.     End Sub
  52.  
  53.     Private Sub ddlTitles_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlTitles.SelectedIndexChanged
  54.         ' Retrieve the DataTable object from the session variable.
  55.         Dim dt As DataTable = DirectCast(Session("TitlesDataTable"), DataTable)
  56.         ' Prepare the Titles variable and do the data binding.
  57.         Titles = dt.Rows(ddlTitles.SelectedIndex)
  58.         Me.DataBind()
  59.     End Sub
  60. End Class
  61.